package com.linkage.educloud.ah.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.config.AccountConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zzhou.common.library.util.ToastShow;

/**
 * Created by Mr'Tang on 2018/12/27.
 */

public class ShareUtils {
    public static void share_qq(Activity activity, String address, String img, String title, String content) {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(activity, mPermissionList, 123);
        }

        if (AccountConfig.getLastAccount() == null) {
            ToastShow.show(activity, "游客不能分享");
            return;
        } else {
            if (!UMShareAPI.get(activity).isInstall(activity, SHARE_MEDIA.QQ)) {
                ToastShow.show(activity, "您没有安装QQ");
                return;
            }
        }

        UMImage image;
        if (img.isEmpty()) {
            image = new UMImage(activity, img);
        } else {
            image = new UMImage(activity, R.mipmap.ic_launcher);
        }
        UMWeb web = new UMWeb(address);
        web.setTitle(title);
        web.setThumb(image);
        web.setDescription(content.length() > 15 ? content.substring(0, 14) : content);
        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.QQ)
                .withMedia(web)
                .share();
    }


    public static void share_wechat(Activity activity, String address, String img, String title, String content) {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(activity, mPermissionList, 123);
        }

        if (AccountConfig.getLastAccount() == null) {
            ToastShow.show(activity, "游客不能分享");
            return;
        } else {
            if (!UMShareAPI.get(activity).isInstall(activity, SHARE_MEDIA.WEIXIN)) {
                ToastShow.show(activity, "您没有安装微信");
                return;
            }
        }

        UMImage image;
        if (img.isEmpty()) {
            image = new UMImage(activity, img);
        } else {
            image = new UMImage(activity, R.mipmap.ic_launcher);
        }
        UMWeb web = new UMWeb(address);
        web.setTitle(title);
        web.setThumb(image);
        web.setDescription(content.length() > 15 ? content.substring(0, 14) : content);
        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .withMedia(web)
                .share();
    }

    public static void share_circle(Activity activity, String address, String img, String title, String content) {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(activity, mPermissionList, 123);
        }

        if (AccountConfig.getLastAccount() == null) {
            ToastShow.show(activity, "游客不能分享");
            return;
        } else {
            if (!UMShareAPI.get(activity).isInstall(activity, SHARE_MEDIA.WEIXIN)) {
                ToastShow.show(activity, "您没有安装微信");
                return;
            }
        }

        UMImage image;
        if (img.isEmpty()) {
            image = new UMImage(activity, img);
        } else {
            image = new UMImage(activity, R.mipmap.ic_launcher);
        }
        UMWeb web = new UMWeb(address);
        web.setTitle(title);
        web.setThumb(image);
        web.setDescription(content.length() > 15 ? content.substring(0, 14) : content);
        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                .withMedia(web)
                .share();
    }
}
